import GlobalData from "./GlobalData";
import NetAdapter from "./adpapter/NetAdapter";
import { GameVoManager } from "./manager/GameVoManager";
import { Util } from "./utils/Util";

const { ccclass, property } = cc._decorator;

@ccclass
export default class SyncToServer extends cc.Component {


    // onLoad () {}

    start() {
        // this.uploadUserinfo();
        this.schedule(this.uploadUserinfo, 10);
    }

    update(dt) {
        // this.uploadUserinfo();
    }

    uploadUserinfo() {
        if (!GlobalData.inst.useNetWork) { return; }
        if (!GlobalData.inst.uid) { return; }
        if (!GameVoManager.getInstance.stateVo.isGetData) { return; }
        let data = GameVoManager.getInstance.myUserVo.serializeAll();
        let stringGlobalUserinfo = JSON.stringify(GlobalData.inst.userinfo);
        if (data == stringGlobalUserinfo) {
            // console.log("无需上传用户数据");
        } else {
            // console.log("data", data);
            // console.log("stringGlobalUserinfo", stringGlobalUserinfo);
            GlobalData.inst.userinfo = JSON.parse(data);
            NetAdapter.setUserinfoPost({ game_data: data }).then(res => {
                Util.logExt(res, "上传用户数据成功");
            }).catch(err => {
                Util.logExt(err, "上传用户数据失败");
            })
        }
    }
}
